package com.geekchique.runningbuddy.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

import com.geekchique.runningbuddy.R;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Point;

public class MapOfJog extends MapActivity {
	
  /** Called when the activity is first created. */
  private String[][] locations = { { "Area 51", "-115.800155,37.248040" },
          { "Bill Gates' house", "-122.242135,47.627787" },
          { "Shepshed Dynamo Football Grounds", "-1.286913,52.774472" },
          { "Michael Jackson's Neverland Ranch", "-120.088012,34.745527" },
          { "Leaning Tower of Pisa", "10.396473,43.723002" },
          { "Airplane Graveyard", "-110.834026,32.150899" },
          { "Grand Canyon", "-112.298641,36.142788" },
          { "Lake Kariba", "27.990417,-17.235252" },
          { "White House", "-77.036519,38.897605" },
          { "World Trade Center site", "-74.012253,40.711641" },
          { "Las Vegas Strip", "-115.162296,36.133347" } };
  
  private Spinner spinner;
  private MapView map;
  private MapController mc;
          
  @Override
  public void onCreate(Bundle icicle) {
      super.onCreate(icicle);
      
      setContentView(R.layout.mapview);
      Button button = (Button) this.findViewById(R.id.go);
      spinner = (Spinner) this.findViewById(R.id.spinner1);
      map = (MapView) findViewById(R.id.map);
      mc = map.getController();
      
      ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(
              this, android.R.layout.simple_spinner_dropdown_item);
      for (int i = 0; i < locations.length; i++)
          adapter.addObject(locations[i][0]);
      adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      spinner.setAdapter(adapter);
      gotoSelected();
      
      button.setOnClickListener(new View.OnClickListener() {
          public void onClick(View arg0) {
              gotoSelected();
          }
      });
     
}
  public void gotoSelected() {
      int pos = spinner.getSelectedItemIndex();
      String[] loc = locations[pos][1].split(",");
      double lat = Double.parseDouble(loc[1]);
      double lon = Double.parseDouble(loc[0]);
      Point p = new Point((int) (lat * 1000000), (int) (lon * 1000000));
      mc.animateTo(p);
      mc.zoomTo(18);
      if (!map.isSatellite())
          map.toggleSatellite();
      map.invalidate();
  }



  
  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
      switch (keyCode) {
      case KeyEvent.KEYCODE_I:
          mc.zoomTo(map.getZoomLevel() + 1);
          break;
      case KeyEvent.KEYCODE_O:
          mc.zoomTo(map.getZoomLevel() - 1);
          break;
      }
      return super.onKeyDown(keyCode, event);
  }
}